package pe.com.nextel.rms;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.kxml2.io.KXmlParser;
import org.kxml2.io.KXmlSerializer;
import pe.com.nextel.util.List;


/**
 * Clase abstracta que deben implementar las clase Bean que sus datos seran guardados
 * en un Record Store
 * Permite almacenar mas de un objeto por registro, para eso se utiliza
 * el List.
 * @author eespinoza
 * @version 1.2, 30/11/2007
 */
public abstract class PersistenteX extends Persistente {
    
    
    /**
     * @deprecate
     */
    public  void persistir(DataOutput dataOutput, List list) throws IOException{
        
    }
    /**
     * @deprecate
     */
    public  List recuperar(DataInput dataInput) throws IOException{
        return null;
    }
    
    
    private int idRecodset;
    
    /**
     *
     * @param dataOutput
     * @param list
     * @throws java.io.IOException
     */
    public abstract void persistir(DataOutputStreamX  dataOutput, List list) throws IOException;
    
    /**
     *
     * @param dataInput
     * @return
     * @throws java.io.IOException
     */
    public abstract List recuperar(DataInputStreamX  dataInput) throws IOException;
    
    /**
     *
     * @param list
     * @return
     * @throws java.io.IOException
     */
    public byte[] persistir(List list) throws IOException {
        ByteArrayOutputStream output = null;
        DataOutputStreamX dataOutput = null;
        try {
            output = new ByteArrayOutputStream();
            dataOutput = new DataOutputStreamX(output);
            
            persistir(dataOutput, list);
            
            return output.toByteArray();
        } finally {
            try {
                if (dataOutput != null) {
                    dataOutput.flush();
                    dataOutput.close();
                    dataOutput = null;
                }
                if (output != null) {
                    output.flush();
                    output.close();
                    output = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * Reads in a record from the given byte array.
     * <br>
     * Calls the abstract method {@link #populate populate()} to populate the record.
     * @param byteArray The byte array that was read in.
     * @throws IOException If there is a problem with creating the streams
     */
    public List readObject(byte[] byteArray)
    throws IOException {
        ByteArrayInputStream byteInputStream = null;
        DataInputStreamX dataInputStream = null;
        
        try {
            byteInputStream = new ByteArrayInputStream(byteArray);
            dataInputStream = new DataInputStreamX(byteInputStream);
            return recuperar(dataInputStream);
        } finally {
            try {
                if (byteInputStream != null) {
                    byteInputStream.close();
                    byteInputStream = null;
                }
                if (dataInputStream != null) {
                    dataInputStream.close();
                    dataInputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * Obtiene el RecordId donde se almacena el objeto
     * @return el recordID
     */
    public int getIdRecodset() {
        return idRecodset;
    }
    
    /**
     * Ingresa el recordID donde se almacena el objeto
     * @param idRecodset
     */
    public void setIdRecodset(int idRecodset) {
        this.idRecodset = idRecodset;
    }
    
    public byte[] writeXML() throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        KXmlSerializer writer = new KXmlSerializer();
        OutputStreamWriter outWriter = null;
        try {

            outWriter = new OutputStreamWriter(out);

            writer.setOutput(outWriter);

            writeXMLObject(writer);
            writer.endDocument();
            
            return out.toByteArray();
            
        } finally {
            if (out != null) {
                out.close();
                out = null;
            }
            if (outWriter != null) {
                outWriter.close();
                outWriter = null;
            }
            if (writer != null) {
                writer = null;
            }
            
        }
    }
    
    /**
     *
     * @param dataOutput
     * @param list
     * @throws java.io.IOException
     */
    public abstract void writeXMLObject( KXmlSerializer writer) throws IOException;
    
    /**
     *
     * @param parser
     * @throws java.io.IOException
     */
    public abstract void readXMLObject(KXmlParser parser) throws IOException;
}
